package controllers;

import models.*;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Security;
import views.html.*;

import java.util.List;

public class Application extends Controller {

    @Security.Authenticated(Secured.class)
    public static Result index() {
        return ok(index.render(
                User.find.byId(request().username())
        ));
    }
    @Security.Authenticated(Secured.class)
    public static Result quiz() {
        List<Spurningar> spurning = Spurningar.find.all();
        List<Svor> svar = Svor.find.all();
        return ok( quiz.render(spurning, svar, User.find.byId(request().username()) ) );
    }

    @Security.Authenticated(Secured.class)
    public static Result submitQuiz(){
        List<Spurningar> spurn = Spurningar.find.all();
        int score = 0;
        for(Spurningar sp : spurn){
            if(form().bindFromRequest().get(String.valueOf(sp.id)).equals("true")){
                score += 1;
            }
        }
        Highscores score1 = new Highscores(request().username(),score);
        score1.save();
         return redirect(routes.Application.scoreboard());
    }
    @Security.Authenticated(Secured.class)
    public static Result scoreboard(){
        List<Highscores> skor = Highscores.find.all();
        return ok(scoreboard.render(User.find.byId(request().username()),skor  ) );
    }
    @Security.Authenticated(Secured.class)
    public static Result kort(){
        return ok(kort.render(User.find.byId(request().username())));
    }

    public static Result Leikur(){
        return ok(Leikur.render());
    }


}